<template>
  <div class="app-container">
    <el-form :model="queryParam" ref="queryForm" :inline="true">
      <el-form-item label="试卷ID：" >       
          <el-input v-model="queryParam.id" ref="paperId" width="30px" placeholder="请输入试卷Id" clearable></el-input>    
      </el-form-item>
      <el-button type="primary" @click="selectpaperId">查询</el-button>
      &nbsp;
      <el-form-item label="年级：">
        <el-select v-model="queryParam.level" placeholder="年级"  @change="levelChange" clearable>
          <el-option v-for="item in levelEnum" :key="item.key" :value="item.key" :label="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学科：">
        <el-select v-model="queryParam.subjectId" placeholder="学科" @change="selectsubjectId" clearable> 
          <el-option v-for="item in subjectFilter" :key="item.courseId" :value="item.courseId" :label="item.courseName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">查询所有试卷</el-button>
        <el-popover placement="bottom" trigger="click">
        
        </el-popover>
      </el-form-item>
    </el-form>
      <el-table :data="tableData.slice((currentPage-1)*pageSize,currentPage*pageSize)" border fit highlight-current-row style="width: 100%">
      <el-table-column prop="paperId" label="Id" width="90px"/>
      <el-table-column prop="courseId" label="学科" width="120px"/>
      <el-table-column prop="one" label="选择题数量"  width="100px"/>
      <el-table-column prop="two" label="判断题数量"  width="100px"/>
      <el-table-column prop="three" label="问答题数量"  width="100px"/>
      <el-table-column prop="paperName" label="试卷名" show-overflow-tooltip/>
      <el-table-column prop="grade" label="年级"  width="50px"/>
      <el-table-column prop="score" label="分数" width="60px"/>
      <el-table-column prop="time" label="创建时间" width="160px"/>
      <el-table-column label="操作" align="center" width="300px">
        <template slot-scope="{row}">
          <el-button size="mini"   @click="showPaper(row)">预览</el-button>
          <el-button size="mini"   @click="submitPaper(row)">发布</el-button>
          <el-button size="mini"  @click="editPaper(row)">编辑</el-button>
          <el-button size="mini"  type="danger" @click="deletePaper(row)" class="link-left">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination align='center' @size-change="handleSizeChange" @current-change="handleCurrentChange" 
            :current-page="currentPage" 
            :page-sizes="[2,5,10,20]" 
            :page-size="pageSize" 
            layout="total, sizes, prev, pager, next, jumper" 
            :total="tableData.length">
            </el-pagination>
    <el-dialog :visible.sync="paperShow.dialog" style="width: 100%;height: 100%">
      <PaperShow :paper="paperShow.paper" :qLoading="paperShow.loading"/>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapState, mapActions } from 'vuex'
import Pagination from '@/components/Pagination'
import PaperShow from '@/components/showExam'


export default {
  components: { Pagination, PaperShow },
  data () {
    return {

      editUrlEnum :[
        {key :'1',
        name:'单选题',
        value:'question/edit/single'} ,
         {key :'2',
        name:'多选题',
        value:'question/edit/mmultiple'},
        {key :'3',
        name:'问答题',
        value:'question/edit/essay'},              
        {key :'4',
        name:'判断题',
        value:'question/edit/judgment'},
      ],

      levelEnum:[
      { key :'1',value:'一年级'},
      { key :'2',value:'二年级'},
      { key :'3',value:'三年级'},
      { key :'4',value:'四年级'},
      { key :'5',value:'大学年级'},
       ] ,
             subjectFilter: [ 
       ] ,
      queryParam: {
        id: null,
        level: null,
        subjectId: null,
        pageIndex: 1,
        pageSize: 10
      },
          questionType: [
       { key :'1',value:'单选题'},
      { key :'2',value:'多选题'},
      { key :'3',value:'简答题'},
      { key :'4',value:'判断题'},
       ] ,

      listLoading: true,
      tableData: [
      ],
      currentPage: 1, // 当前页码
      total: null, // 总条数
      pageSize: 2 // 每页的数据条数
      ,
      paperShow: {
        dialog: false,
        paper: null,
        loading: false
      }
    }
  },
    created () {
  },
    mounted(){
      this.getcourseId();
      this.search();
    },
  methods: {
    //每页条数改变时触发 选择一页显示多少行
    handleSizeChange(val) {
                    console.log(`每页 ${val} 条`);
                    this.currentPage = 1;
                    this.pageSize = val;
                },
                //当前页改变时触发 跳转其他页
                handleCurrentChange(val) {
                    console.log(`当前页: ${val}`);
                    this.currentPage = val;
                },
    //获取所有课程信息
    getcourseId(){
        this.$http.get("course/get").then(res =>{
          console.log("加载数据成功")
          this.subjectFilter=res.data;
          console.log(this.subjectFilter[1].courseId)
        })
      },
    selectpaperId(){
      this.tableData = [];//清空当前页数据，不如查询数会push进当前页面数据
      var paperId = this.$refs.paperId.value;
      //在请求执行成功后执行回调函数中的内容，回调函数处于其它函数的内部this不会与任何对象绑定，为undefined
      this.$http.get("get/paper?id="+paperId).then(
        re=>{
           const res = re.data;//大题问题，查询试卷1无定义
           if(re.data.paperId != undefined){
            //输入不存在id传进来的是未定义，不是未空
          this.tableData.push(res);//eleui表格是用数组来排序，如果是数据是对象，要将对象转为数组
          console.log("加载数据成功")
           }else{
             this.$message.error("试卷不存在")
             console.log("加载数据失败")
           }
    })
  },
    selectsubjectId(value){
      var courseId = value;
      this.$http.get("paper/courseId?courseId="+courseId).then(
         re=>{
           const res = re;
           if(res!==null){

          this.tableData =res.data;
                  console.log(res.list instanceof Array)
          console.log("加载数据成功")
           }else{
             console.log("加载数据失败")
           }
    })
  },
    submitForm () {
      this.queryParam.pageIndex = 1
      this.search()
    },
    search () {
      this.listLoading = true
         this.$http.get("paper/get").then(
         re=>{
           const res = re
           if(res!==null){
          this.tableData =res.data
          console.log("加载数据成功")
           }else{
             console.log("加载数据失败")
           }
         }
     );  
    },
    levelChange () {
      var grade = this.queryParam.level
      this.$http.get("paper/grade?grade="+grade).then(
         re=>{
           const res = re
           if(res!==null){
          this.tableData =res.data
          console.log("加载数据成功")
           }else{
             console.log("加载数据失败")
           }
         }
     );  
    },
    addQuestion () {
      this.$router.push('/exam/question/edit/singleChoice')
    },
    showPaper (row) {
      let _this = this
      this.paperShow.dialog = true
      this.paperShow.loading = true
   
 this.$http.get("get/paper?id="+row.paperId).then(
         re=>{
        _this.paperShow.paper = re.data
        _this.paperShow.loading = false
        console.log(re.data.titleItems)
         })
    },
    submitPaper(row){
      if(row.status == 1){
        this.$message.error("您已经发布过来，请不要再次操作")
        return false
      }
      this.$http.post("commit/paper?paperId="+row.paperId).then(re => {
        if (re.data != null) {
          this.$message.success("发布成功")
        } else {
          this.$message.error("发布失败")
        }
      })
    },
    editPaper (row) {
      if(row.status == 1){
        this.$message.error("该试卷已经发布，请勿修改")
        return false
      }
        this.$router.push({ path: 'updateExam' ,query: { id: row.paperId }})
    },
    deletePaper (row) {
      this.$http.get("paper/delete/"+row.paperId).then(re => {
        if (re.data != null) {
          this.$message.success(re.message)
        } else {
          this.$message.error(re.message)
        }
      })
      this.search()
    },
    questionTypeFormatter (row, column, cellValue, index) {
      return this.enumFormat(this.questionType, cellValue)
    },
    subjectFormatter (row, column, cellValue, index) {
      return this.subjectEnumFormat(cellValue)
    },
  }

}
</script>
